package com.gzxx.domain.bo;

import com.gzxx.constant.SystemConstant;
import com.gzxx.domain.base.RequestPage;
import com.gzxx.domain.po.SysUser;
import com.gzxx.domain.po.SysUserDept;
import com.gzxx.domain.po.SysUserRole;
import com.gzxx.util.SysLoginUserHelper;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.Assert;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author MAX
 * @date 2020/7/22 3:22 下午
 */
@Builder
@Getter
@ToString
public class SysUserBO {

    private Long id;
    private String userAccount;
    private String userPassword;
    private String employeeName;
    private String nickName;
    private String email;
    private String mobile;
    private Integer gender;
    private String avatar;
    private Long createBy;
    private Date createTime;
    private Long updateBy;
    private Integer accountStatus;
    private RequestPage page;
    /**
     * 多条件搜索
     */
    private String searchCondition;
    /**
     * 角色ID
     */
    private Long roleId;
    private List<Long> roleIdList;

    /**
     * 一级部门id
     */
    private Integer firstDeptId;
    /**
     * 二级部门id
     */
    private Integer secondDeptId;


    public Map<?, ?> queryParams() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return BeanMap.create(this);
    }

    /**
     * 创新系统用户
     *
     * @return SysUser
     */
    public SysUser persist() {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return SysUser.builder()
                .userAccount(this.userAccount)
                .userPassword(passwordEncoder.encode(this.userPassword))
                .employeeName(this.employeeName)
                .nickName(this.nickName)
                .email(this.email)
                .gender(this.gender)
                .mobile(this.mobile)
                .avatar(this.avatar)
                .createBy(SysLoginUserHelper.getLoginUserId())
                .build();
    }

    public SysUser edit() {
        Assert.state(this.id > 0L, "SysUser must provide update identifier.");

        return SysUser.builder()
                .id(this.id)
                .email(this.email)
                .mobile(this.mobile)
                .employeeName(this.employeeName)
                .nickName(this.nickName)
                .avatar(this.avatar)
                .updateBy(SysLoginUserHelper.getLoginUserId())
                .build();
    }

    /**
     * 修改密码
     *
     * @return SysUser
     */
    public SysUser changePassword() {
        Assert.state(this.id > 0L, "SysUser id must greater than zero.");
        Assert.hasLength(this.userPassword, "SysUser password must not be null.");

        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return SysUser.builder()
                .id(this.id)
                .userPassword(passwordEncoder.encode(this.userPassword))
                .build();
    }

    /**
     * 创建用户角色关系
     */
    public SysUserRole createUserRole(Long userId) {
        return SysUserRole.builder()
                .userId(userId)
                .roleId(this.roleId)
                .build();
    }

    /**
     * 初始化密码
     *
     * @return
     */
    public SysUser initPassword() {
        Assert.state(this.id > 0L, "SysUser id must greater than zero.");

        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return SysUser.builder()
                .id(this.id)
                .userPassword(passwordEncoder.encode(SystemConstant.INIT_USER_PASSWORD))
                .updateBy(SysLoginUserHelper.getLoginUserId())
                .build();
    }

    /**
     * 更改角色状态
     */
    public SysUser changeStatus() {
        Assert.state(this.id > 0L, "SysUser must provide update identifier.");
        return SysUser.builder()
                .id(this.id)
                .accountStatus(this.accountStatus)
                .updateBy(SysLoginUserHelper.getLoginUserId())
                .build();
    }


    /**
     * 创建用户部门关系
     */
    public SysUserDept createUserDept(Long userId) {
        SysUserDept userDept = SysUserDept.builder()
                .userId(userId)
                .firstDeptId(this.firstDeptId)
                .secondDeptId(this.secondDeptId)
                .build();
        return userDept;
    }

}
